package com.itzz.spring_zhoneHeng.controller;

import com.itzz.spring_zhoneHeng.base.RestResponse;
import com.itzz.spring_zhoneHeng.dao.BookDao;
import com.itzz.spring_zhoneHeng.dao.ClassificationDao;
import com.itzz.spring_zhoneHeng.dao.OrderDao;
import com.itzz.spring_zhoneHeng.pojo.Book;
import com.itzz.spring_zhoneHeng.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    BookService bookService;

    @Autowired
    BookDao bookDao;

    @Autowired
    OrderDao orderDao;



    //查询所有的书籍
    @RequestMapping("/queryAllBook")
    public RestResponse queryAllBook(Book book, Integer pageNum, Integer pageSize){
        RestResponse response = bookService.queryAllBook(book, pageNum, pageSize);
        return response;
    }

    //根据id回显书籍
    @RequestMapping("/queryById/{id}")
    public RestResponse queryById(@PathVariable("id") Integer id){
        RestResponse response = bookService.queryById(id);
        return response;
    }

    //修改书籍
    @RequestMapping("/update/{id}")
    @PreAuthorize("hasRole('ROLE_super')")
    public RestResponse update(@PathVariable("id") Integer id, Book book, MultipartFile files,Integer userId){
        if(id!=null&&id!=0){
            book.setId(id);
        }
        RestResponse response = bookService.updateBook(book,files,userId);
        return response;
    }

    //根据id删除书籍
    @RequestMapping("/delete/{id}")
    @PreAuthorize("hasRole('ROLE_super')")
    public RestResponse delete(@PathVariable("id") Integer id){
        RestResponse response = bookService.deleteById(id);
        return response;
    }

    //添加书籍(不是添加到书架)
    @RequestMapping("/add")
    @PreAuthorize("hasRole('ROLE_super')")
    public RestResponse add(Book book, MultipartFile files,Integer classId){
        RestResponse response = bookService.addBook(book, files,classId);
        return response;
    }

    //根据bookid 查询表b_chap 表中的章节id 根据章节id 查询章节表chapters
    // /book/queryChapids/{bookId}
    @RequestMapping("/queryChapids/{bookId}")
    public RestResponse queryChapids(@PathVariable("bookId") Integer bookId){
        RestResponse response = bookService.queryChapids(bookId);

        return response;

    }


    //根据书籍id查询书籍的信息 /book/queryByBook/{bookId}
    @RequestMapping("/queryByBook/{bookId}")
    public RestResponse queryByBook(@PathVariable("bookId") Integer bookId){
        Book book = bookDao.queryByBook(bookId);
        if(book!=null&&!book.equals("")){
            return  RestResponse.ok(book.getBookName());
        }
        return  RestResponse.fail(0,"查询失败");
    }
}
